var ip = window.location.hostname
var itemInfo
var fightRecord
var team1, team2

function postfunc(route) {
    var args, callback
    if (arguments.length > 2) {
        args = arguments[1]
        callback = arguments[2]
    } else if (arguments.length == 2) {
        args = {}
        callback = arguments[1]
    }
    var url = "http://" + ip + ":" + 8001 + "/" + route
    $.post(url, args, function(data, status) {
        console.log(data)
        callback(data)
    }, "json")
}

$(document).ready(function() {
    $(".button").click(function() {
        var route = $(this).prevAll('p').text()
        var url = route
        var args = {}
        $(this).parent('div').find("input").each(function() {
            if ($(this).attr('name')) {
                args[$(this).attr('name')] = $(this).val()
            }
        })
        $(this).parent('div').find("select").each(function() {
            if ($(this).attr('name')) {
                args[$(this).attr('name')] = $(this).val()
            }
        })
        console.log(args)
        $.post(url, args, function(data, status) {
            console.log(data)
        }, "json")
    })

    $("#getplayer").click(function() {
        getplayer()
    })

    $("#getRecordFight").click(function() {
        getRecordFight()
    })

    $("#fight").click(function() {
        getRecordFight()
        var route = 'player/battleOnMap'
        postfunc(route, function(data) {
            if (data.error == 0) {
                fight(data.fightRecord)
            }
        })
    })

    $("#business").click(function() {
        var route = 'service/merchant'
        postfunc(route, function(data) {
            if (data.error == 0) {
                $('.business .tmp').empty()
                var content = ''
                var merchant = data.merchant
                for (var m in merchant) {
                    var but = "<button onclick='buy(" + m + ")'>购买</button>"
                    content += '<p  align="left">' + 'id:' + m + but + '</p>'
                }
                $('.business .tmp').append(content)
            }
        })
    })
})

function getplayer() {
    var route = 'player/getPlayer'
    postfunc(route, function(data) {
        if (data.error == 0) {
            $('.bag .tmp').empty()
            $('.itemInfo .tmp').empty()
            var player = data.player
            for (var b in player.basicAttri) {
                $(".basicAttri ." + b).val(player.basicAttri[b])
            }
            $(".basicAttri .lvl").val(player.lvl)
            $(".basicAttri .exp").val(player.exp + '/' + player.lvlexp)
            $(".basicAttri .money").val(player.money)
            $(".basicAttri .mapId").val(player.mapId)

            for (var f in player.fightAttri) {
                $(".fightAttri ." + f).val(player.fightAttri[f])
            }

            $(".bag .size").val(player.bag.size)
            $(".bag .empty").val(player.bag.empty)
            var content = ''
            var items = player.bag.items
            for (var i in items) {
                var but = "<button onclick='sell(" + items[i][0] + ")'>出售</button>"
                if (items[i][0] < 300000) {
                    but += "<button onclick='relieve(" + items[i][0] + ")'>解印</button>"
                }
                if (items[i][0] > 1000000) {
                    but += "<button onclick='equip(" + items[i][0] + ")'>装备</button>"
                }
                content += '<p  align="left">' + 'id:' + items[i] + but + '</p>'
            }
            $('.bag .tmp').append(content)

            for (var e in player.equipments) {
                $(".equipments ." + e).val(player.equipments[e])
                $(".equipments ." + e).append("<button onclick='unload(" + player.equipments[e] + ")'>卸下</button>")
            }
            var content = ''
            itemInfo = player.itemInfo
            for (var i in itemInfo) {
                var but = "<button onclick='getItemInfo(" + i + ")'>查看</button>"
                content += '<p  align="left" class="info' + i + '">' + 'id:' + i + but + '</p>'
            }
            $('.itemInfo .tmp').append(content)
        }
    })
}

function sell(id) {
    var route = 'service/sell'
    var args = {
        commodityId: id,
        number: 1
    }
    postfunc(route, args, function(data) {
        getplayer()
    })
}

function buy(id) {
    var route = 'service/buy'
    var args = {
        commodityId: id
    }
    postfunc(route, args, function(data) {
        getplayer()
    })
}

function equip(id) {
    var route = 'service/equip'
    var args = {
        equipmentid: id,
    }
    postfunc(route, args, function(data) {
        getplayer()
    })
}

function unload(part) {
    var route = 'service/unload'
    var args = {
        part: part,
    }
    postfunc(route, args, function(data) {
        getplayer()
    })
}

function relieve(id) {
    var route = 'service/relieve'
    var args = {
        equipmentid: id
    }
    postfunc(route, args, function(data) {
        getplayer()
    })
}

function getItemInfo(id) {
    $('.info').remove()
    var str = ''
    var item = itemInfo[id]
    for (var i in item) {
        if (i === 'attri') {
            var attri = item[i]
            for (var a in attri) {
                str += '<p>' + a + ':' + attri[a] + '</p>'
            }
        } else {
            str += '<p>' + i + ':' + item[i] + '</p>'
        }
    }
    var content = '<div class="info">' + str + '</div>'
    $(".info" + id).append(content)
}

function getRecordFight() {
    $(".recordFight .tmp").empty()
    var route = 'player/getFightRecord'
    postfunc(route, function(data) {
        if (data.error == 0) {
            fightRecord = data.fightRecord
            var content = ''
            for (var f in fightRecord) {
                content += '<p>' + f + '<button onclick="fight(' + f + ')">查看战斗</button></p>'
            }
            $(".recordFight .tmp").append(content)
        }
    })
}

function fight(record) {
    $(".fight .tmp").empty()
    if (typeof record === 'string' || typeof record === 'number') {
        record = JSON.parse(fightRecord[parseInt(record)])
    }
    team1 = record.team1
    team2 = record.team2
    var fightActions = record.fightActions
    var results = record.results

    var length = fightActions.length
    var i = 0
    battling()

    function battling() {
        var fightAction = fightActions[i]
        i++
        var content = ''
        content = getNameByPos(fightAction.attackerRecord.pos) + "使用了技能" + fightAction.attackerRecord.skillId
        if (fightAction.attackerRecord.pos === 2) {
            $('.fight .tmp').prepend('<p  align="right">' + content + '</p>')
        } else {
            $('.fight .tmp').prepend('<p  align="left">' + content + '</p>')
        }
        for (var k = 0; k < fightAction.defenderRecords.length; k++) {
            if (fightAction.defenderRecords[k].oper.reacOper === 2) {
                content = getNameByPos(fightAction.defenderRecords[k].pos) + "闪避了" + getNameByPos(fightAction.attackerRecord.pos) + "的攻击。"
                if (fightAction.defenderRecords[k].pos === 2) {
                    $('.fight .tmp').prepend('<p  align="right">' + content + '</p>')
                } else {
                    $('.fight .tmp').prepend('<p  align="left">' + content + '</p>')
                }
                continue
            }
            var critContent = fightAction.defenderRecords[k].oper.reacOper === 1 ? "暴击" : ""
            content = getNameByPos(fightAction.defenderRecords[k].pos) + "受到了" + getNameByPos(fightAction.attackerRecord.pos) + "的" + fightAction.defenderRecords[k].oper.damage + critContent + "伤害"

            if (fightAction.defenderRecords[k].pos === 2) {
                $('.fight .tmp').prepend('<p  align="right">' + content + '</p>')
            } else {
                $('.fight .tmp').prepend('<p  align="left">' + content + '</p>')
            }
        }

        if (i < length) {
            setTimeout(battling, 300)
        } else {
            if (record.results) {
                $('.fight .tmp').prepend('<p  align="left">' + 'exp:' + record.results[0].exp + '</p>')
                $('.fight .tmp').prepend('<p  align="left">' + 'money:' + record.results[0].money + '</p>')
                for (var t in record.results[0].items) {
                    $('.fight .tmp').prepend('<p  align="left">' + 'items:' + record.results[0].items[t] + '</p>')
                }
            }
            getplayer()
        }
    }

    function getNameByPos(pos) {
        for (var t in team1) {
            if (pos === team1[t].pos) {
                return team1[t].name
            }
        }
        for (var t in team2) {
            if (pos === team2[t].pos) {
                return team2[t].name
            }
        }
    }
}